<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/10/3
 * Time: 20:08
 */
namespace app\api\controller;

use app\api\model\Carinfor as CarinforModel;
use think\Request;

class Carinfor extends Base{

    /**
     * Notes: 查询车辆信息
     * Create Date: 2018/10/2
     * Create by :lijie
     */
    public function carInfo(Request $request){
        $car    = new CarinforModel();

        // 接收数据并验证
        $req = $request->post();
        $resVali = $this->validate($req,"CarInfoValidate.carInfo",'');
        if($resVali !== true){
            return apiResponse(201,$resVali,'');
        }
        $id = $req['id'];    //obd id

        // 车辆信息
        $w_car['o_id'] = $id;
        $info = $car->carInfoByUser($w_car);
        if($info){
            return apiResponse(200,'获取成功',$info);
        }else {
            return apiResponse(201,'获取失败','');
        }
    }

    /**
     * Notes: 商户修改obd车辆信息
     * Create Date: 2018/10/2
     * Create by :lijie
     */
    public function editCarInfo(Request $request){
        $car    = new CarinforModel();
        // 获取数据并验证
        $param = $request->post();
        $resVali = $this->validate($param,"CarInfoValidate.editCarInfo");
        if($resVali !== true){
            return apiResponse(201,$resVali,'');
        }
        $c_id = input('post.c_id');
        $o_id = input('post.o_id');
        $w['status'] = 0;
        if(!empty($c_id)){
            $w['c_id']   = $c_id;
        }
        if(!empty($o_id)){
            $w['o_id']   = $o_id;
            // obd下是否绑定车辆
            $carInfo = $car->carInfoByUser($w);
            if(!$carInfo){
                return apiResponse(201,'该obd设备下没有绑定车辆','');
            }
        }

        $res = $car->editCarInfo($w,$param);
        if($res){
            return apiResponse(200,'修改成功','');
        }else {
            return apiResponse(201,'修改失败','');
        }
    }

    /**
     * Notes: 用户批量删除车辆信息
     * Create Date: 2018/10/2
     * Create by :lijie
     * 参数：c_ids         // 车辆id数组    c_ids = 1,2,3
     */
    public function delCarInfor(Request $request){
        $car    = new CarinforModel();

        // 验证
        $param = $request->post();
        $resVali = $this->validate($param,"CarInfoValidate.delCarInfor");
        if($resVali !== true){
            return apiResponse(201,$resVali,'');
        }
        $c_ids = $param['c_ids'];
        $c_ids = explode(',',$c_ids);

        $c_cid = count($c_ids);
        for($i=0;$i<$c_cid;$i++){
            $w['c_id']      = $c_ids[$i];
            $save['status'] = 9;
            $res = $car->editCarInfo($w,$save);
        }

        if($res){
            return apiResponse(200,'修改成功',$res);
        }else {
            return apiResponse(201,'修改失败',$res);
        }
    }

}